/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.market.web.controller;

import com.market.web.EJB.AllItems;
import com.market.web.EJB.CustomerFacade;
import com.market.web.EJB.InventoryFacade;
import com.market.web.EJB.OrderTransaction;
import com.market.web.model.Customer;
import com.market.web.model.Item;
import javax.servlet.http.*;

import java.io.*;
import java.util.*;
import javax.ejb.EJB;
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.annotation.WebServlet;




   // @EJB
   // private CustomerFacade customerFacade;

 
/**
 *
 * @author Lee SH
 */

@WebServlet("/MailApp")
public class MailApp extends HttpServlet {

    @EJB
    private InventoryFacade inventoryFacade;
    
    @EJB
    private CustomerFacade customerFacade;

    @EJB
    private OrderTransaction orderTrans;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
       
            HttpSession sess = req.getSession();
            AllItems items  = (AllItems) sess.getAttribute("items");
            Item item;
            int oId = Integer.parseInt(req.getParameter("OrderId"));
            Customer c = (Customer)sess.getAttribute("cust");
            //Customer c = customerFacade.find(4);  //need to change
            
            //System.out.println("Item id--->" + items.getTotal());
            //int oId = orderTrans.makeOrder(c, items);
            
           String to = c.getEmail();
           
          
          String sub = ("CustId:" +c.getCustId() + "Invoice from Global Shopping Pte Ltd ");
          for(int i = 0; i< c.getOrder1Collection().size();i++)
          {
              
              System.out.println();
          }
          
          StringBuilder builder = new StringBuilder();
          

           try (PrintWriter pw = resp.getWriter()) {
           // builder.append("<html><body>");
           builder.append("Thank You for shopping with Us ");
            builder.append(System.getProperty("line.separator"));
            builder.append("Here are your order list!!!! ");
            builder.append(System.getProperty("line.separator"));
            builder.append(System.getProperty("line.separator"));
           // builder.append("<table border='1'>");
          //  builder.append("<tr>");
            builder.append("Name ");
            builder.append("\t\t");
             
        //    builder.append("</td>");
            builder.append("Unit Price ");
              builder.append("\t\t");
               
         //   builder.append("</td>");
            builder.append("Quantity ");
              builder.append("\t\t");
               
          //  builder.append("</td>");
            builder.append("Sub Total ");
             builder.append(System.getProperty("line.separator"));
        //    builder.append("</td>");
        //    builder.append("</tr>");
            for(Item i: items.getItems())
            {
              //  builder.append("<tr>");
                builder.append( i.getName() );
                  builder.append("\t");
                   builder.append("\t");
                     builder.append("\t");
                //  builder.append("\t\t");
               // builder.append("</td>");
                builder.append(i.getPrice());
                  builder.append("\t");
                  builder.append("\t\t");
              //  builder.append("</td>");
                builder.append( i.getQty());
                  builder.append("\t");
                  builder.append("\t\t");
                   builder.append("\t");
             //   builder.append("</td>");
                builder.append(i.getSubTotal());
                builder.append(System.getProperty("line.separator"));
            //   builder.append("</td>");
            //   builder.append("</tr>");
            }
            
            builder.append(System.getProperty("line.separator"));
            builder.append("Grand Total ");
            builder.append("\t\t");
            builder.append( items.getTotal());
            
            
            String html = builder.toString();
           
          
            //pw.println("Invoice Sent Successfully");
            
       
    final String user= "sa37cart@gmail.com";//change accordingly  
    final String pass= "sa37cart1";  
  
//Get the session object    
Properties props = new Properties();  
props.put("mail.smtp.host", "smtp.gmail.com");//change accordingly  
props.put("mail.smtp.auth", "true");  
 props.put("mail.smtp.starttls.enable", "true");
  
Session session = Session.getInstance(props,  
 new javax.mail.Authenticator() {  
  protected PasswordAuthentication getPasswordAuthentication() {  
   return new PasswordAuthentication(user,pass);  
   }  
});  
//compose message  
try {  
 MimeMessage message = new MimeMessage(session);  
 message.setFrom(new InternetAddress("sa37cart@gmail.com"));  
 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
 message.setSubject(sub);  
 message.setText(html);
 
 
 

   
 //send message  
 Transport.send(message);  
  
 System.out.println("Done");  
  
 } catch (MessagingException e) {  
    throw new RuntimeException(e);  
 }  
} 
  
           
           
}}



